package com.mastercard.assets.web.struts.actions;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.mastercard.assets.jpa.TblAsset;
import com.mastercard.assets.jpa.TblCampania;
import com.mastercard.assets.jpa.TblCategoria;
import com.mastercard.assets.jpa.TblCuentaPaquete;
import com.mastercard.assets.jpa.TblPaquete;
import com.mastercard.assets.jpa.TblSubcateg;
import com.mastercard.assets.jpa.TblTipoCampania;
import com.mastercard.assets.jpa.TblTipoCuenta;
import com.mastercard.assets.notifications.NotificationUtil;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.AssetAltaForm;
import com.mastercard.assets.web.struts.forms.AssetAsignarPaquetesForm;
import com.mastercard.assets.web.struts.forms.AssetBorrarForm;
import com.mastercard.assets.web.struts.forms.AssetBuscarForm;
import com.mastercard.assets.web.struts.forms.AssetEditarForm;
import com.mastercard.assets.web.struts.forms.AssetModificarPaquetesForm;
import com.mastercard.assets.web.struts.forms.CampanasPorAutorizarForm;
import com.mastercard.assets.web.struts.forms.GenerarCampanaForm;
import com.mastercard.assets.web.struts.forms.GenerarCampanaRegistrarForm;
import com.mastercard.assets.web.struts.forms.LoginForm;
import com.mastercard.assets.web.util.to.ManejadorPaquetesAsignados;
import com.mastercard.assets.web.util.to.PaqueteAutorizado;
import com.mastercard.assets.web.util.to.UserLogin;

public class CampanasPorAutorizarRegistrar
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el form
		CampanasPorAutorizarForm f = ( CampanasPorAutorizarForm ) form ;
		
		System.out.println( "CampanasPorAutorizarRegistrar.java: f/" + f ) ;
		
		// en caso deque no sea null ni el formulario ni el bc y no venga de la pagina 3 y operation 1
		if( f != null && f.getBc( ) != null && f.getBc( ).getFileName( ).trim( ).length( ) > 0 && f.getPage( ) != 3 && f.getOperation( ) != 1 )
		{
			// toma el bc
			FormFile bc = f.getBc( ) ;
			int cveCampania = f.getCveCampania( ) ;
			
			System.out.println( "CampanasPorAutorizarRegistrar.java: bc/" + bc ) ;
			
			try
			{
				// en caso dr que no sea null
				String name = bc.getFileName( ) ;
				String ext = name.substring( name.lastIndexOf( "." ) + 1 ) ;
				
				
				// guarda el archivo en /WEB-INF/bc
				String bcName = "BusinessCase-" + cveCampania + "." + ext ;
				
				// toma el File
				String dirPath = getServlet( ).getServletContext( ).getRealPath( "/WEB-INF/bc" ) ;
				System.out.println( dirPath + System.getProperty( "file.separator" ) + bcName ) ;
				
				MCUtil.dropAllBCOfCampania( dirPath , cveCampania ) ;
				
				try
				{
					FileOutputStream out = new FileOutputStream( dirPath + System.getProperty( "file.separator" ) + bcName , false ) ;
					out.write( bc.getFileData( ) ) ;
					out.close( ) ;
					
					MasterCardDAO.setBCOfCampaign( cveCampania , bcName ) ;
				}catch( Exception e )
				{
                                    MasterCardDAO.closeEntityManager( ) ;
					e.printStackTrace( ) ;
					
					user.setMessageError( MCUtil.messages.getString( "com.mastercard.assets.web.modules.autorizarcampana.errors.Error" , e.getMessage( ) ) ) ;
					
					// en cuanto se vaya a rechar se dirige al detalle
					return mapping.findForward( "success" ) ;
				}
				
                                MasterCardDAO.closeEntityManager( ) ;
				// en cuanto se vaya a rechar se dirige al detalle
				return mapping.findForward( "success" ) ;
			}catch( Exception e )
			{
                            MasterCardDAO.closeEntityManager( ) ;
				e.printStackTrace( ) ;
				user.setMessageError( e.getMessage( ) ) ;
				return mapping.findForward( "Error" ) ;
			}
		}
		
		System.out.println( "/CampanasPorAutorizarRegistrarIn.do?operation=" + request.getParameter( "operation" ) + "&cveCampania=" + request.getParameter( "cveCampania" ) + "&page=" + request.getParameter( "page" ) ) ;
		user.setNextWorkArea( "/CampanasPorAutorizarRegistrarIn.do?operation=" + request.getParameter( "operation" ) + "&cveCampania=" + request.getParameter( "cveCampania" ) + "&page=" + request.getParameter( "page" ) ) ;
		
                MasterCardDAO.closeEntityManager( ) ;
		return mapping.findForward( "success" ) ;
	}
	
}
